<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分配</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <!-- 添加分类 -->
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
                </el-col>
            </el-row>
            <!-- 表单 -->
            <tree-table show-index :show-row-hover="false" index-text="#" border :expand-type="false"
                :selection-type="false" :data="catelist" :columns="columns">
                <template slot="index" slot-scope="scope">
                    {{ scope.row.cat_id }}
                </template>
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted == false" style="color:lightskyblue"></i>
                    <i class="el-icon-error" v-else="scope.row.cat_deleted == true"></i>
                </template>
                <template slot="order" slot-scope="scope">
                    <el-tag v-if="scope.row.cat_level == 0">标签一</el-tag>
                    <el-tag type="success" v-else-if="scope.row.cat_level == 1">标签二</el-tag>
                    <el-tag type="info" v-else>标签三</el-tag>
                </template>
                <!-- 操作区 -->
                <template slot="operate" slot-scope="scope">
                    <el-button icon="el-icon-edit" type="primary" @click="CategoriesEdit(scope.row.cat_id)">编辑</el-button>
                    <el-button icon="el-icon-delete-solid" type="danger" @click="CategoriesDelete(scope.row.cat_id)">删除
                    </el-button>
                </template>
            </tree-table>
            <!-- 添加对话框 -->
            <el-dialog title="提示" :visible.sync="CategoriesVisible" width="30%" @close="addCateDialogClosed">
                <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="分类名称" prop="cat_name">
                        <el-input v-model="ruleForm.cat_name"></el-input>
                    </el-form-item>
                    <el-form-item label="父级分类">
                        <!-- options 用来指定数据源 -->
                        <!-- props 用来指定配置对象 -->
                        <el-cascader :options="parentCateLists" :props="cascaderProps" v-model="selectedKeys"
                            @change="handleChange" clearable change-on-select>
                        </el-cascader>

                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="CategoriesVisible = false">取 消</el-button>
                    <el-button type="primary" @click="SelectparamsList">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 修改对话框 -->
            <el-dialog title="提示" :visible.sync="CategoriesEditVisible" width="30%" @close="addCateDialogClosed">
                <el-form :model="EditruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="要修改的id:" prop="cat_id">
                        <el-input :value="EditruleForm.cat_id" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="分类名称" prop="cat_name">
                        <el-input v-model="EditruleForm.cat_name"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="CategoriesEditVisible = false">取 消</el-button>
                    <el-button type="primary" @click="CategoriesEditClick">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 分页区 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total" align="center">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import { Select } from 'element-ui'

export default {
  data() {
    return {
      // 定义分页数据
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 总条数
      total: 0,
      // 商品分类数据
      catelist: [],
      // 定义添加分类对话框是否弹出
      CategoriesVisible: false,
      // 为table 指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        }, {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        }, {
          label: '排序',
          type: 'template',
          template: 'order'
        }, {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      // 表单数据
      ruleForm: {
        // 将要添加的名称
        cat_name: '',
        // 父级分类的id
        cat_pid: 0,
        // 分类的等级 默认要添加的是1 级分类
        cat_level: 0
      },
      // 添加分类表单的验证规则对象
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      // 父级分类的列表
      parentCateLists: [],
      // 指定级联选择器的配置对象
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 选中的父级分类的Id数组
      selectedKeys: [],
      // 要修改的 名称
      EditruleForm: {
        cat_name: '',
        cat_id: 0
      },
      // 设置修改对话的是否显示
      CategoriesEditVisible: false
      // 保存要修改的id

    }
  },
  created() {
    this.gteCateList()
  },
  methods: {
    // 获取商品分类数据
    async gteCateList() {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.$message({
        message: '获取成功',
        type: 'success'
      })
      this.catelist = res.data.result
      this.total = res.data.total
    },
    /// 处理页码大小
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.gteCateList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.gteCateList()
    },
    // 父级分类的列表
    async parentCateList() {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.$message({
        message: '获取成功',
        type: 'success'
      })
      this.parentCateLists = res.data
      console.log(this.paramsList)
    },

    // 编辑
    async CategoriesEdit(id) {
      this.EditId = id
      this.CategoriesEditVisible = true
      // 发起修改请求
      const { data: res } = await this.$http.get('categories/' + this.EditId)
      this.EditruleForm.cat_name = res.data.cat_name
      this.EditruleForm.cat_id = res.data.cat_id
    },
    // 获取连级的值
    handleChange() {
      // 判断是否选择了子分类
      if (this.selectedKeys.length > 0) {
        // 父级分类的id
        this.ruleForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // 为当前分类等级赋值
        this.ruleForm.cat_level = this.selectedKeys.length
      } else {
        // 父级分类的id
        this.ruleForm.cat_pid = 0
        // 为当前分类等级赋值
        this.ruleForm.cat_level = 0
      }
    },
    // 添加添加按钮
    showAddCateDialog() {
      // 点击添加按钮 获取数据
      this.parentCateList()
      // 显示对话框
      this.CategoriesVisible = true
    },
    // 确认添加按钮
    async SelectparamsList() {
      // 请求添加请求
      const { data: res } = await this.$http.post('categories', this.ruleForm)
      if (res.meta.status !== 201) return this.$message.error('创建失败')
      this.$message.success('创建成功')
      // 刷新页面
      this.gteCateList()
      // 关机对话框
      this.CategoriesVisible = false
    },
    // 监听对话框是否关闭
    addCateDialogClosed() {
      // 对话框关闭回调函数
      this.ruleForm.cat_level = 0
      this.ruleForm.cat_name = ''
      this.ruleForm.cat_pid = 0
      this.selectedKeys = null
    },
    // 确认修改
    async CategoriesEditClick() {
      console.log(this.EditruleForm.cat_name)
      const { data: res } = await this.$http.put('categories/' + this.EditruleForm.cat_id, {
        cat_name: this.EditruleForm.cat_name
      })
      if (res.meta.status !== 200) return this.$message.error('修改失败')
      this.$message.success('修改成功')
      // 刷新页面
      this.gteCateList()
      this.CategoriesEditVisible = false
    },
    // 删除分类
    async CategoriesDelete(id) {
      const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      if (confirm !== 'confirm') return this.$message.error('取消删除')
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      // 刷新页面
      this.gteCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-button {
  margin: 10px 0 !important;
}
</style>
